package org.jeecg.modules.ws.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.ws.dto.ExportSheetDataDto;
import org.jeecg.modules.ws.dto.UploadSheetDataDto;
import org.jeecg.modules.ws.entity.WsBondInfo;
import org.jeecg.modules.ws.entity.WsBondProject;
import org.jeecg.modules.ws.mapper.WsBondInfoMapper;
import org.jeecg.modules.ws.service.IWsBondInfoService;
import org.jeecg.modules.ws.service.IWsBondProjectService;
import org.jeecg.modules.ws.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Description: 债券信息管理
 * @Author: jeecg-boot
 * @Date: 2025-03-27
 * @Version: V1.0
 */
@Service
public class WsBondInfoServiceImpl extends ServiceImpl<WsBondInfoMapper, WsBondInfo> implements IWsBondInfoService {
    @Autowired
    private IWsBondProjectService bondProjectService;
    @Autowired
    private ExcelUtil excelUtil;


    @Override
    public Result<?> uploadExcel(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<String> list1 = new ArrayList<>();
        list1.add("bond_code");
        List<String> list2 = new ArrayList<>();
        list2.add("bond_code");
        list2.add("project_code");
        List<UploadSheetDataDto<?>> sheetDataList = new ArrayList<>();
        sheetDataList.add(new UploadSheetDataDto<WsBondInfo>(WsBondInfo.class, this, list1));
        sheetDataList.add(new UploadSheetDataDto<WsBondProject>(WsBondProject.class, bondProjectService, list2));
        return excelUtil.importExcel(request, response, sheetDataList);
        // excelUtil.importExcel(request, response, WsBondInfo.class, WsBondProject.class,this, bondProjectService,list1,list2);
    }

    @Override
    public ModelAndView exportSampleExcel(HttpServletRequest request, WsBondInfo wsBondInfo) {
        QueryWrapper<WsBondInfo> queryWrapper = QueryGenerator.initQueryWrapper(wsBondInfo, request.getParameterMap());
        List<WsBondInfo> list1 = this.list(queryWrapper);
        List<WsBondProject> list2 = new ArrayList<>();
        list1.forEach(bondInfo -> {
            String bondCode = bondInfo.getBondCode();
            List<WsBondProject> bondProjectList = bondProjectService.queryByBondCode(bondCode);
            list2.addAll(bondProjectList);
        });
        /*Map<List, Class> map = new LinkedHashMap<>();
        map.put(list1, WsBondInfo.class);
        map.put(list2, WsBondProject.class);*/
        List<ExportSheetDataDto<?>> sheetDataList = new ArrayList<>();
        sheetDataList.add(new ExportSheetDataDto<WsBondInfo>(list1, WsBondInfo.class,"债券信息"));
        sheetDataList.add(new ExportSheetDataDto<WsBondProject>(list2, WsBondProject.class,"债券项目信息"));
        List xls = excelUtil.packageSheet(sheetDataList);
        return excelUtil.exportXls("债券信息管理",xls);
    }

    @Override
    public WsBondInfo queryByBondCode(String bondCode) {
        LambdaQueryWrapper<WsBondInfo> query = Wrappers.lambdaQuery(WsBondInfo.class);
        query.eq(WsBondInfo::getBondCode,bondCode);
        return getOne(query);
    }
}
